<template>
	<div style="height: 500px;width: 250px; border: 1px solid red;">
		<h2 style="text-align: center;">登录</h2>
		<table border="1px">
			<tr>
				<td>账号</td>
				<td><input v-model="user.phone" /></td>
			</tr>
			<tr>
				<td>密码</td>
				<td><input v-model="user.pwd" /></td>
			</tr>
			<tr>
				<td></td>
				<td><button @click="logins()">登录</button></td>
			</tr>
		</table>

	</div>
</template>
<script setup>
import axios from 'axios';
</script>
<script>

export default {
	data() {
		return {
			user: {}
		}
	},
	methods: {
		logins() {
			if (this.user.phone == undefined || this.user.pwd == undefined) {
				alert("登录不允许为空")
				return;
			}
			var a = ''
			var fd = new FormData()
			fd.append("username", this.user.phone)
			fd.append("password", this.user.pwd)
			var than = this;
			this.$axios.post(this.$ip + "/api/login", fd).then(function (resd) {
				a = "Bearer " + resd.data.data.token
				window.localStorage.setItem("token", a)
				console.log(resd.data.data.token)
				console.log(a)
				than.$axios.get(than.$ip + "/api/user/info", { headers: { 'Authorization': a } }).then(function (resd) {
					than.$store.state.user = resd.data.data
					than.$router.push("/" + resd.data.data.role)


					console.log(resd.data)
				})


			})
		}
	},
	mounted() {

	}
}
</script>

<style></style>
